package com.sjq.project.service.impl;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sjq.common.entity.LoginAccount;
import com.sjq.common.util.FTPClientHelper;
import com.sjq.common.util.PageResults;
import com.sjq.common.util.PrimaryKeyUtil;
import com.sjq.project.dao.ProjectFlowDao;
import com.sjq.project.dao.ProjectDao;
import com.sjq.project.entity.ProjectFlow;
import com.sjq.project.entity.Project;
import com.sjq.project.service.ProjectService;
import com.sjq.project.util.ProjectConstant;

@Service
public class ProjectServiceImpl implements ProjectService{
	@Resource
	private ProjectDao dao;
	@Resource
	private ProjectFlowDao flowDao;
	/**新增  **/
	@Override
	public JSONObject addProject(Project project,LoginAccount loginAccount) {
		/**标准注册登记人信息***/
		project.setDjdw_gajgjgdm(loginAccount.getUserDwbm());
		project.setDjr_xm(loginAccount.getUserName());
		project.setDjr_yhm(loginAccount.getUserId());
		int count=dao.addProject(project);
		JSONObject json =new JSONObject();
		if(count>0){
			//插入流程记录表
			ProjectFlow entity=new ProjectFlow();
			entity.setXxzjbh(PrimaryKeyUtil.getUUID());
			entity.setGlrw_xxzjbh(project.getXxzjbh());
			entity.setLc_mc(ProjectConstant.LCMC_1);
			entity.setLcczr_xm(loginAccount.getUserName());
			entity.setLcczr_jh(loginAccount.getAccountId());
			entity.setLcczr_sfhm(loginAccount.getUserId());
			entity.setLcczr_dwmc(loginAccount.getUserDwmc());
			entity.setLcczr_dwbm(loginAccount.getUserDwbm());
			entity.setLc_czsj("");
			entity.setNext_lc("");
			entity.setLc_nr("");
			flowDao.addFlow(entity);
			
			json.put("code","200");
			json.put("msg","ok");
			//蓝信注册通知
			
		}else{
			json.put("code","300");
			json.put("msg","error");
		}
		return json;
	}
	/**新增含有附件的文档上传至FTP***/
	@Override
	public JSONObject addProjectFilesByFTP(Project project, CommonsMultipartFile[]  fj,LoginAccount loginAccount) {
		/*
		project.setDjdw_gajgjgdm(loginAccount.getUserDwbm());
		project.setDjdw_dwmc(loginAccount.getUserDwmc());
		project.setDjr_gmsfhm(loginAccount.getUserId());
		project.setDjr_xm(loginAccount.getUserName());
		*/
		String remotePath="\\"+loginAccount.getUserId()+"\\";//远程登录默认同步至当前身份证号码文件夹下面
		FTPClientHelper ftp=new FTPClientHelper(remotePath);
		for(int i=0;i<fj.length;i++){
			String originalFileName=fj[i].getOriginalFilename();//获取原文件名称
			String suffix=	originalFileName.substring(originalFileName.lastIndexOf("."));//获取源文件类型
			String newFileName=PrimaryKeyUtil.getUUID()+""+suffix;	//将文件重新命名
			boolean flage=false;
			try {
				flage = ftp.upload(fj[i].getInputStream(), newFileName);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//是否上传成功
			if(flage){
				/***上传成功，将信息插入数据库***/
				String url=remotePath+newFileName;//
				/*
				project.setUrl(url);//设置文件存储路径
				*/
				
			}else{
				//上传文件失败
			}
		}
		
		int count=dao.addProject(project);	
			
		JSONObject json =new JSONObject();
		if(count>0){
			json.put("code","200");
			json.put("msg","ok");
		}else{
			json.put("code","300");
			json.put("msg","error");
		}
		return json;
	}
	
	
	
	/**删除 **/
	@Override
	public JSONObject deleteProject(String projectId,LoginAccount loginAccount) {
		int count=dao.deleteProject(projectId);
		JSONObject json =new JSONObject();
		if(count>0){
			json.put("code","200");
			json.put("msg","ok");
		}else{
			json.put("code","300");
			json.put("msg","error");
		}
		return json;
	}
	
	/**更新 **/
	@Override
	public JSONObject updateProject(Project project,LoginAccount loginAccount) {
		//跟新人信息
		/*
		project.setXgr_gajgjgdm(loginAccount.getUserDwbm());
		project.setXgr_dwmc(loginAccount.getUserDwmc());
		project.setXgr_gmsfhm(loginAccount.getUserId());
		project.setXgr_xm(loginAccount.getUserName());
		*/
		JSONObject result=new JSONObject();
		int count=dao.updateProject(project);
		if(count>0){
			result.put("msg", "success");
			result.put("code","200");
		}else{
			result.put("msg", "error");
			result.put("code","300");
		}
		return result;
	}
	
	/**查询**/
	@Override
	public PageResults<?> findProjectByPage(int pageNo, int pageSize, Project project) {
		PageResults<?> pageResults=dao.selectProjectByPage(pageNo, pageSize, project);
		return pageResults;
	}
	
	/**根据查询条件查询所有的数据**/
	@Override
	public JSONObject findProjectByListAll(Project project,LoginAccount la){
		List<Map<String, String>> list=dao.findProjectByListAll(project);
		/**
		* 权限校验
		**/
		JSONObject result=new JSONObject();
		result.put("msg", "success");
		result.put("code","200");
		result.put("data",list);
		return result;
	}
	/*****/
	
	
}